package com.hkts.management.message.msg.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.message.msg.entity.MsgRecord;
import com.hkts.management.message.msg.service.IMsgRecordService;
import com.hkts.management.message.utils.CustomPageUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author zy
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/message/msgRecord")
public class MsgRecordController {
    private final Logger logger = LoggerFactory.getLogger(MsgRecordController.class);

    @Autowired
    public IMsgRecordService iMsgRecordService;

    /**
     * 分页查询数据
     *
     * @param page      分页信息
     * @param msgRecord 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getMsgRecordPageList")
    public Result<Object> getHontUserList(Page<MsgRecord> page, MsgRecord msgRecord) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iMsgRecordService.selectPage(page, msgRecord)));
    }

    /**
     * 保存和修改公用的
     *
     * @param msgRecord 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgRecordSave")
    public Result<Object> msgRecordSave(MsgRecord msgRecord) {
        iMsgRecordService.saveOrUpdate(msgRecord);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/msgRecordGetById")
    public Result<Object> msgRecordGetById(String id) {
        MsgRecord byId = iMsgRecordService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgRecordDelete")
    public Result<Object> hontUserDelete(String id) {
        iMsgRecordService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgRecordBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iMsgRecordService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    /**
     * 获取通知列表
     */
    @ApiOperation(value = "获取通知列表", notes = "获取通知列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "消息ID", paramType = "path"),
            @ApiImplicitParam(name = "size", required = true, value = "查询数量", paramType = "path"),
            @ApiImplicitParam(name = "sysType", required = true, value = "消息类型", paramType = "path")
    })
    @ResponseBody
    @GetMapping(value = "/getRecordList")
    public Result<List<MsgRecord>> getRecordList(String id,Integer size,Integer sysType){
       return iMsgRecordService.getRecordList(id,size,sysType);
    }
    @ApiOperation(value = "获取时间段通知列表", notes = "获取时间段通知列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", required = true, value = "起始时间", paramType = "path"),
            @ApiImplicitParam(name = "endTime", required = true, value = "结束时间", paramType = "path"),
            @ApiImplicitParam(name = "sysType", required = true, value = "消息类型", paramType = "path")
    })
    @ResponseBody
    @GetMapping(value = "/getRecordsByTime")
    public Result<List<MsgRecord>> getRecordsByTime(LocalDateTime startTime, LocalDateTime endTime, Integer sysType){
       return iMsgRecordService.getRecordsByTime(startTime,endTime,sysType);
    }

    /**
     * 修改通知状态为已读
     */
    @ResponseBody
    @GetMapping(value = "/changeRecordStatus")
    public Result<Object> changeRecordStatus(String id){
        return iMsgRecordService.changeRecordStatus(id);
    }
}
